# coding: utf8
# testing 1234

#######################################################
# advertise that our plugin is installed and available
plugin_plugin_installed = True

# coding: utf8
import os

def ensure_file(path,content=None):
    d = os.path.dirname(path)
    try:
        os.stat(path)
    except:
        if not os.path.exists(d):
            os.makedirs(d)
        path = open(path, 'w')
        if not content == None:
            print('provided a template')
            our_text = content
            path.writelines(our_text)
            path.close()


def ensure_dir(path):
    d = os.path.dirname(path)
    try:
        os.stat(path)
    except:
        if not os.path.exists(d):
            os.makedirs(d)

#########################################
# initialization.py
import os
cwd = os.getcwd()
current_app = os.path.join(cwd,'applications',request.application)
initialization_file_path = os.path.join(current_app,'models','000_uc_initialization.py')

####################################
# do we hav an initialization file?
if not 'initialization_file_created' in globals():
    if not os.path.exists(initialization_file_path):
        our_file = open(initialization_file_path,"w")
        our_text = '############################################\n'
        our_text += '# file generated by models/plugin_plugin.py\n'
        our_text += '#\n'        
        our_text += 'initialization_file_created = True\n'
        our_text += '##################################\n'
        our_text += '# [plugin_plugin]\n'
        our_file.writelines(our_text)
        our_file.close()
        print('created %s' % initialization_file_path)
    else:
        print('found %s' % initialization_file_path)
else:
    print('found "initialization_file_created" in globals')
    
#######################################
# ensure for plugin_plugin_admin group
#
if not 'plugin_plugin_admin_group_created' in globals():
    our_file = open(initialization_file_path,"a")
    our_text = 'plugin_plugin_admin_group_created = True\n'
    our_file.write(our_text)
    our_file.close()
    if not db(db.auth_group.role=='plugin_plugin_admin').select(): 
        db.auth_group.insert(role='plugin_plugin_admin',description='group for administrating the plugin plugin_plugin')
    else:
        print("found db.auth_group.role=='plugin_plugin_admin' in database")        
else:
    print('found "plugin_plugin_admin_group_created" in globals')

#######################################
# ensure for plugin_plugin_config group
#
if not 'plugin_plugin_config_group_created' in globals():
    our_file = open(initialization_file_path,"a")
    our_text = 'plugin_plugin_config_group_created = True\n'
    our_file.write(our_text)
    our_file.close()
    if not db(db.auth_group.role=='plugin_plugin_config').select(): 
        db.auth_group.insert(role='plugin_plugin_config',description='group for configuring the plugin plugin_plugin')
    else:
        print("found db.auth_group.role=='plugin_plugin_config' in database")        
else:
    print('found "plugin_plugin_config_group_created" in globals')

#########################################
# plugin_plugin menu

response.menu+= [
    (T('plugin menu'), False, URL('plugin_plugin','index'),
        [
            (T('plugin options'), False, URL('plugin_plugin','index'),
                [
                    (T('create_plugin'), False, URL('plugin_plugin', 'create_plugin')),
                ]),
            (T('about'), False, URL(request.application,'plugin_plugin','display_static',args='about')),
            (T('contribute'), False, URL(request.application,'plugin_plugin','display_static',args='contribute')),  
            (T('license'), False, URL(request.application,'plugin_plugin','display_static',args='license')),
            (T('universal cake'), False, URL(request.application,'plugin_plugin','display_static',args='universal_cake')),
            (T('voice of access'), False, URL(request.application,'plugin_plugin','display_static',args='voice_of_access')),
        ]
   )]

##############################################
# site plugin_plugin users menu
##############################################
# plugin_plugin menu that is displayed to registered signed in users
if auth.is_logged_in():
    response.menu+= [    
    (T('plugin_plugin user menu'), False, URL('plugin_plugin','index'),
        [
            (T('plugin_plugin user actions'), False, URL('plugin_plugin','index'),
                [
                    (T('configure plugin'), False, URL('plugin_plugin', 'configure')),
                ]),          
        ]
   )]    

##############################################
# site plugin_plugin administrators menu
##############################################
# plugin_plugin menu that is displayed to registered signed in users who
# are also members of the admin_plugin_plugin group.
if auth.is_logged_in() and auth.has_membership(auth.id_group('admin_plugin_plugin')):
#        response.menu_admin_plugin = []
#        response.menu_admin_plugin += [
    response.menu+= [    
        ('plugin_plugin admin', False, URL('plugin_plugin','admin'),
       )]
#        response.menu_plugin += response.menu_admin_plugin
